package POI;



import java.io.*;

import javax.servlet.*;

import POI.GeoInfo;
import POI.GeoInfoList;
import java.util.*;



public class GeoServlet extends GenericServlet {
	
	private static final long serialVersionUID = -191123335401967525L;
	
	private boolean wasError;
	GeoInfoList body;
		
	public GeoServlet()
	{
		wasError=false;
		body = new GeoInfoList();
	}
	
	public String createResponse(String filePath)
	{
		if(!body.loadFromFile(filePath)) wasError=true;
		String result = new String();
		result = "";
        if(wasError)
        	result = "Error reading from CSV file!";
        else 
        {
        	if(body.isEmpty())
        		result = "CSV file is empty!";
        	else
        	{
        	    Iterator<GeoInfo> it = body.iterator();
        		while(it.hasNext())
        		{
        			GeoInfo val = it.next();
        			result += val.toString()+"<br>";
        		}
        	}
        };
    	return result;
	}
        	

	public void service(ServletRequest request, ServletResponse response) 
	throws ServletException, IOException {
		response.setContentType("text/html");
        PrintWriter out = response.getWriter();
		out.println(createResponse("GeoServlet\\poidata.csv"));
        out.close();

	}

}
